/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.gui;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import edu.yonsei.iwb.sdt.SDTLearner;
import edu.yonsei.iwb.sdt.SDTLearningType;
import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import edu.yonsei.iwb.sdt.ontology.JenaModelType;
import edu.yonsei.iwb.sdt.ontology.OntologyModel;
import edu.yonsei.iwb.sdt.util.SDTLog;
import edu.yonsei.iwb.sdt.util.UtilFunctions;
import java.io.IOException;


/**
 *
 * @author DK
 */
public class Controller {
    
    private MainFrame mf;
    private String ontology;
    private OntologyModel ontmodel;
    private int learningType = SDTLearningType.DEFAULT;
    private double threshold = 0;
    private Resource targetClass;
    private Resource targetProperty;    
    private Model model;
    private SDTLearner sdtl;
    private DNode root;
    
    private Vector<Resource> posset;
    private Vector<Resource> negset;
    
    
    public Controller(MainFrame mf){
        this.mf = mf;       
    }
    
    public Model getJenaModel(){
        return this.model;
    }
    
    public void setPOSNEG_Instances(Vector<String> pos, Vector<String> neg){
        
//        SDTLog.log(SDTLog.DETAIL, "Positive Instances:\n");                
        StringBuffer ps = new StringBuffer();
        StringBuffer ns = new StringBuffer();
        this.posset = new Vector<Resource>();
        this.negset = new Vector<Resource>();
        for (int i = 0; i < pos.size(); i++) {
            Resource posr = this.model.createResource(pos.get(i));
            this.posset.add(posr);
            ps.append(posr.toString()+"\n");            
        }
                
        for (int i = 0; i < neg.size(); i++) {
            Resource negr = this.model.createResource(neg.get(i));
            this.negset.add(negr);            
            ns.append(negr.toString()+"\n");
        }
        
//        SDTLog.log(SDTLog.DETAIL, ps.append(ns));      
        
    }            
    
    public void selectOntFile(String ontology){
        this.ontology = ontology;
    }
    
    public Vector<String> getClassTypeResource(){
        Vector<String> classSet = new Vector<String>();
        
        ResIterator res = this.model.listResourcesWithProperty(RDF.type, OWL.Class);
        
        while(res.hasNext()){
            String s = res.nextResource().toString();
            classSet.add(s);
        }            
        
        return classSet;
    }
    
    public Vector<String> getPropertyResource(String classuri){
        Vector<String> propertySet = new Vector<String>();
        
        Resource r = this.model.createResource(classuri);
        ResIterator res = this.model.listResourcesWithProperty(RDFS.domain, r);
        while(res.hasNext()){
            Resource p = res.nextResource();
//            StmtIterator stmt = this.model.listStatements(p, RDFS.range, XSD.xboolean);
//           
//            while(stmt.hasNext()){
//                Statement s = stmt.nextStatement();
            propertySet.add(p.toString());
//            }
        }        
        return propertySet;
    }

    public void loadOntOnBaseDIR_TDB() {
//        JenaModelType.MODEL_DIR = directory;
        ontmodel = new OntologyModel(JenaModelType.TDB_MODEL);
        ontmodel.loadOntology(ontology);
        this.model = this.ontmodel.getJenaModel();
    }
    
//    public void loadOntOnNEWDIR_TDB(String directory) {
//        JenaModelType.MODEL_DIR = directory;
//        ontmodel = new OntologyModel(JenaModelType.TDB_MODEL);
//        ontmodel.loadOntology(ontology);
//    }

    public void useExistTDBModel(String directory) {
        JenaModelType.MODEL_DIR = directory;
        ontmodel = new OntologyModel(JenaModelType.TDB_MODEL);
        this.model = this.ontmodel.getJenaModel();
    }
    
    public void useOntOnMemory(){
        ontmodel = new OntologyModel(JenaModelType.MEMORY_MODEL);
        ontmodel.loadOntology(ontology);
        this.model = this.ontmodel.getJenaModel();
    }
    
    public void setTargetClass(String s){
        this.targetClass = this.model.createResource(s);
    }
    
    public void setTargetProperty(String s){
        this.targetProperty = this.model.createResource(s);
    }
    
    public Vector<Resource> getTargetInstances(){
        ResIterator res = this.model.listResourcesWithProperty(RDF.type, this.targetClass);
        Vector<Resource> tiset = UtilFunctions.getSubjectResourceVector(res);
        return tiset;
    }
    
    public void setThreshold(double e){
        this.threshold = e;
    }
    
    public void setLearningType(int e){        
            this.learningType = e;        
    }
    
    public void defineSDTLearner(int type) throws IOException{
        if(type == CurrentMainFrameStatus.TARGET_PROPERTY_USED){            
            this.sdtl = new SDTLearner(this.targetClass.toString(), this.targetProperty.toString(), this.ontmodel, this.learningType);
//            SDTLog.setLogType(SDTLog.BASIC);
            this.sdtl.setENTROPY_THRESHOLD(this.threshold);
            this.root = sdtl.learningStart();
            this.mf.svp.showGraph(this.root);
            
        }else if(type == CurrentMainFrameStatus.TARGET_INSTANCE_USED){
            this.sdtl = new SDTLearner(this.targetClass.toString(), this.posset,this.negset, this.ontmodel, this.learningType);
//            SDTLog.setLogType(SDTLog.BASIC);
            this.sdtl.setENTROPY_THRESHOLD(this.threshold);
            this.root = sdtl.learningStart();
            this.mf.svp.showGraph(this.root);
        }       
    
    }

    
}
